<template>
  <div>
    <el-form :model="queryParams" ref="queryForm" size="mini" v-show="showSearch" label-width="68px">
      <el-row :gutter="20">
        <el-col :span="4">
          <el-form-item label="项目区域" prop="projectRegion">
            <el-select v-model="queryParams.projectRegion" placeholder="请选择" clearable
              @keyup.enter.native="handleQuery">
              <el-option v-for="dict in dict.type.cld_project_region" :key="dict.value" :label="dict.label"
                :value="parseInt(dict.value)"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="项目名称" prop="projectName">
            <el-input v-model="queryParams.projectName" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="员工姓名" prop="staffName">
            <el-input v-model="queryParams.staffName" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="消耗时间">
            <el-date-picker v-model="dateRange" value-format="yyyy-MM-dd" type="daterange" range-separator="-"
              start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label-width="10px">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['cld:materialConsume:add']">添加消耗信息</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="materialConsumeList" @selection-change="handleSelectionChange" border>
      <el-table-column label="报损时间" align="center" prop="consumeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.consumeTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="项目区域" align="center" prop="projectRegion">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_project_region" :value="scope.row.projectRegion" />
        </template>
      </el-table-column>
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="岗位编号" align="center" prop="projectPostNo" />
      <el-table-column label="岗位" align="center" prop="projectPostName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_project_post" :value="scope.row.projectPostName" />
        </template>
      </el-table-column>
      <el-table-column label="组别" align="center" prop="groupName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cld_project_groups" :value="scope.row.groupName" />
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center" prop="staffName" />
      <el-table-column label="性别" align="center" prop="staffSex" />
      <el-table-column label="联系方式" align="center" prop="staffPhone" />
      <el-table-column label="消耗数量" align="center" prop="totalNumber" />
      <el-table-column label="总价" align="center" prop="totalMoney" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleView(scope.row)"
            v-hasPermi="['cld:materialConsume:query']">消耗明细</el-button>
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)"
            v-hasPermi="['cld:materialConsume:edit']">修改</el-button>
          <el-button size="mini" type="text" @click="handleDelete(scope.row)"
            v-hasPermi="['cld:materialConsume:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />


    <!-- 消耗明细 -->
    <el-dialog :title="viewTitle" :visible.sync="viewOpen" width="60%" append-to-body>
      <!--商品数据-->
      <el-table :data="form.detailsList" border>
        <el-table-column label="商品类别" align="center" prop="commodityType" />
        <el-table-column label="商品编号" align="center" prop="commodityNo" />
        <el-table-column label="商品名称" align="center" prop="commodityName1" />
        <el-table-column label="规格" width="80" align="center" prop="commoditySpec" />
        <el-table-column label="单位" width="150" align="center" prop="unitName1" />
        <el-table-column label="数量" width="120" align="center" prop="commodityNumber" />
        <el-table-column label="单价" width="120" align="center" prop="costPrice">
          <template slot-scope="scope">
            {{ toMoney(scope.row.costPrice) }}
          </template>
        </el-table-column>
        <el-table-column label="总价" width="120" align="center" prop="totalMoeny">
          <template slot-scope="scope">
            {{ toMoney(scope.row.totalMoeny) }}
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="viewOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMaterialConsume,
  getMaterialConsume,
  delMaterialConsume,
} from "@/api/cld/materialConsume";

export default {
  name: "MaterialConsume",
  dicts: ["cld_project_region", "cld_project_post", "cld_project_groups"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      dateRange: [],
      // 总条数
      total: 0,
      // 消耗记录表格数据
      materialConsumeList: [],
      // 弹出层标题
      viewTitle: "",
      // 是否显示弹出层
      viewOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        consumeType: 1,
        projectRegion: null,
        projectName: null,
        staffName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询消耗记录列表 */
    getList() {
      this.loading = true;
      listMaterialConsume(
        this.addDateRange(this.queryParams, this.dateRange)
      ).then((response) => {
        this.materialConsumeList = response.rows;
        this.materialConsumeList.forEach((i) => {
          let totalNumber = 0;
          let totalMoney = 0;
          i.detailsList.forEach((item) => {
            totalNumber += item.commodityNumber;
            totalMoney += item.commodityNumber * item.costPrice;
          });
          i.totalNumber = totalNumber;
          i.totalMoney = this.toMoney(totalMoney);
        });
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push("/materialConsume/add-consume/addConsume/0");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push(
        "/materialConsume/add-consume/addConsume/" + row.materialConsumeId
      );
    },
    // 消耗明细
    handleView(row) {
      this.form = row;
      this.viewTitle =
        row.staffName +
        "-" +
        this.parseTime(row.consumeTime, "{y}-{m}-{d}") +
        "消耗详情";
      this.viewOpen = true;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const materialConsumeIds = row.materialConsumeId || this.ids;
      this.$modal
        .confirm(
          '是否确认删除消耗记录编号为"' + materialConsumeIds + '"的数据项？'
        )
        .then(function () {
          return delMaterialConsume(materialConsumeIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
  },
};
</script>
<style scoped>
.el-range-editor.el-input__inner {
  width: 100%;
}

.el-select {
  width: 100%;
}
</style>
